package com.liu.service.user;

import com.liu.dao.BaseDao;
import com.liu.dao.user.UserDao;
import com.liu.dao.user.UserDaoImpl;
import com.liu.pojo.User;
import org.junit.jupiter.api.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService {
    //业务层都会调用Dao层,所以我们这里要引入Dao层

    //创建一个UserDao属性，目的是获取数据库连接
    private UserDao userDao;

    //创建一个构造方法，目的是初始化
    public UserServiceImpl(){
        userDao = new UserDaoImpl();
    }
    @Override
    public User login(String userCode, String password) {
        //创建一个连接对象
        Connection connection = null;
        //创建一个User对象，目的是接收数据库查询结果
        User user = null;


        try {
            connection = BaseDao.getConnection();
            //通过业务层调用对应具体数据库操作
            user = userDao.getLoginUser(connection, userCode, password);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            BaseDao.closeResource(connection, null, null);
        }

        return user;
    }

    @Test
    public void test(){
        UserServiceImpl userService = new UserServiceImpl();
        User admin = userService.login("admin", "1234567");
        System.out.println(admin.getAge() + " " + admin.getUserName() + " " + admin.getGender());
    }

    //修改密码
    @Override
    public boolean updatePwd(int id, String pwd) {
        Connection connection = null;
        boolean flag = false;

        try{
            connection = BaseDao.getConnection();
            if (userDao.updatePwd(connection,id,pwd)>0){
                flag = true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    //获取用户

    @Override
    public int getUserCount(String userName, int userRole){
        Connection connection = null;
        int count = 0;
        connection = BaseDao.getConnection();
        try {
            count = userDao.getUserCount(connection,userName,userRole);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.closeResource(connection,null,null);
        }

        return count;
    }

    @Test
    public void test01() {
        UserServiceImpl userService = new UserServiceImpl();
        int count = userService.getUserCount(null,1);
        System.out.println(count);
    }

    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) throws SQLException {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection, queryUserName, queryUserRole, currentPageNo, pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return userList;
    }
}
